package com.example.learn_mybatis.controller;


import com.example.learn_mybatis.pojo.PageParam;
import com.example.learn_mybatis.pojo.Pager;
import com.example.learn_mybatis.pojo.Permissions;
import com.example.learn_mybatis.pojo.Result;
import com.example.learn_mybatis.service.impl.PermissionsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chen
 * @since 2025-07-11
 */
@RestController
@RequestMapping("/permissions")
public class PermissionsController {
    @Autowired
    private PermissionsServiceImpl permissionsService;

    /**
     * 获取所有权限
     * @param pageParam
     * @return
     */
    @GetMapping
    public Result getAllPermissions(PageParam pageParam) {
        Pager allPermissions = permissionsService.getAllPermissions(pageParam.getPage(), pageParam.getSize());
        return Result.success(allPermissions);
    }

    /**
     * 新增权限
     * @param permissions
     * @return
     */
    @PostMapping
    public Result addPermissions(@RequestBody Permissions permissions) {
        permissionsService.save(permissions);
        return Result.success();
    }

    /**
     * 更新权限
     * @param permissions
     * @return
     */
    @PutMapping
    public Result updatePermissions(@RequestBody Permissions permissions) {
        boolean b = permissionsService.updateById(permissions);
        if(b){
            return Result.success();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deletePermissions(Integer id) {
        boolean b = permissionsService.removeById(id);
        if(b){
            return Result.success();
        } else {
            return Result.error();
        }
    }
}
